package controller;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.User;
import model.UserModel;
import model.UserProfile;

public class Register extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//PrintWriter out = response.getWriter();
		HttpSession sessionND = request.getSession();
		HttpSession sessionTB = request.getSession();
		String action = request.getParameter("action");
		
		User UserDTO = new User();
		
		if(action.equals("dangky")){
			String tennd = request.getParameter("txtTenND");
			String tendn = request.getParameter("txtTenDN");
			String matkhau = request.getParameter("txtMatkhau");
			String email = request.getParameter("txtEmail");
			String sdt = request.getParameter("txtSdt");
			String diachi = request.getParameter("txtDiachi");
			UserModel c = new UserModel();
			UserProfile profile = new UserProfile();
			profile.setName(tennd);
			c.setUsername(tendn);
			c.setPassword(matkhau);
			c.setEmail(email);
			profile.setTel(sdt);
			profile.setAddress(diachi);
			try {
				if(UserDTO.checkUser(c.getUsername())){
					String cauTB = "Ten dang nhap da ton tai !!!";
					sessionTB.setAttribute("cdk", cauTB);
					response.sendRedirect("Trang_dang_ky.jsp");
				}
				else{
					try {
						UserDTO.AddUser(c, profile);
					} catch (NoSuchAlgorithmException e) {
						e.printStackTrace();
					}
					String cauTB = "Dang ky thanh cong";
					sessionTB.setAttribute("cdk", cauTB);
					response.sendRedirect("Trang_dang_ky.jsp");
				}
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(action.equals("thoat")){
			sessionND.removeAttribute("nguoidung");
			response.sendRedirect("Trang_chu.jsp");
		}
		
	}
}
